//
//  CommonUtil.m
//  LineByLine
//
//  Created by 병철 김 on 11. 10. 19..
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "CommonUtil.h"

@implementation CommonUtil

+ (void) setKeyValuesTo:(id)keyValueObject first:(id)firstObject valist:(va_list) args {
	int i = 0;
	id key = firstObject;
    for (id arg = firstObject; arg != nil; arg = va_arg(args, id)) {
        if ((++i % 2) == 0) {
			[keyValueObject setValue:arg forKey:key];
		} else {
			key = arg;
		}
    }
}

+ (NSString*) singularWord:(NSString*) word {
    if ([word hasSuffix:@"ies"]) {
        return [NSString stringWithFormat:@"%@y", [word substringToIndex:([word length]-3)]];
    } else if ([word hasSuffix:@"es"]) {
        return [word substringToIndex:([word length]-2)];
    } else {
        return [word substringToIndex:([word length]-1)];
    }
}

+ (NSString *)urlEncodeString:(NSString*) string usingEncoding:(NSStringEncoding)encoding {
	return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                               (CFStringRef)string,
                                                               NULL,
                                                               (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                               CFStringConvertNSStringEncodingToEncoding(encoding));
}
@end
